package com.hzl.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import com.hzl.entity.dto.CategoryDto;
import com.hzl.entity.dto.CategoryUpdateDto;
import com.hzl.entity.pojo.Category;
import com.hzl.entity.vo.CategoryAllPageVo;
import com.hzl.entity.vo.CategoryAllVo;
import com.hzl.mapper.CategoryMapper;
import com.hzl.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author huangzelnag
 * @create 2022-05-04
 */
@Service

public class CategoryServiceImpl implements CategoryService {




    @Autowired
    CategoryMapper categoryMapper;

    /**
     * -新增分类
     *
     * @param categoryDto
     * @return
     */
    @Override
    public Boolean addCategory(CategoryDto categoryDto) {

        Category category = new Category();
        BeanUtils.copyProperties(categoryDto, category);
        //1.判断是否存在相应的数据
        Integer integer = categoryMapper.selectByCategory(category);

        if(integer>0){
            return false;
        }

        //获取id
        Snowflake snowflake = new Snowflake();
        String idStr = snowflake.nextIdStr();
        category.setId(idStr);

        //获取当前时间
        Date date = DateUtil.date();
        category.setCreateTime(date);
        category.setUpdateTime(date);

        //获取当前用户
        category.setCreateUser("1");
        category.setUpdateUser("1");

        return   categoryMapper.insert(category)>0;
    }

    /**
     * 更新分类
     *
     * @param categoryUpdateDto
     */
    @Override
    public Boolean updateCategory(CategoryUpdateDto categoryUpdateDto) {

        Category category = new Category();
        BeanUtils.copyProperties(categoryUpdateDto, category);
        category.setUpdateTime(new Date());
        category.setSort(categoryUpdateDto.getSort()+"");

        return categoryMapper.update(category)>0;
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteCategory(String id) {

        return categoryMapper.deleteById(id)>0;
    }

    /**
     * 获取所有
     *
     * @return
     */
    @Override
    public List<CategoryAllVo> getCategoryAll() {
        List<Category> categories = categoryMapper.selectAll();
        List<CategoryAllVo> list =new ArrayList<>();
        if (CollUtil.isEmpty(categories)) {
            return null;
        }

        for (Category category : categories) {
            if (Objects.isNull(category)) {
                continue;
            }
            CategoryAllVo categoryAllVo = new CategoryAllVo();
            BeanUtils.copyProperties(category, categoryAllVo);
            list.add(categoryAllVo);
        }


        return list;
    }

    /**
     * 根据分页获取所有
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public List<CategoryAllPageVo> getCategoryAllPage(Integer page, Integer pageSize) {
        page=(page-1)*pageSize;

        List<Category> categories = categoryMapper.selectAllPage(page,pageSize);

        List<CategoryAllPageVo> list =new ArrayList<>();
        if (CollUtil.isEmpty(categories)) {
            return null;
        }

        for (Category category : categories) {
            if (Objects.isNull(category)) {
                continue;
            }
            CategoryAllPageVo categoryAllPageVo = new CategoryAllPageVo();
            BeanUtils.copyProperties(category, categoryAllPageVo);
            list.add(categoryAllPageVo);
        }

        return list;
    }


}
